﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
*命名空间：DAL.Base
*文件名： ContextBase
*创建人： kingling
*创建时间：2018/6/19 12:50:47
*描述
*=======================================================================
*修改标记
*修改时间：2018/6/19 12:50:47
*修改人：kingling
*描述：
************************************************************************/

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace DAL.Base
{
    public class ContextBase : DbContext
    {
        public ContextBase(DbContextOptions options) : base(options)
        {
            Database.EnsureCreated();
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var configtypes = this.getCurrentConfigTypes();
            foreach (var item in configtypes)
            {
                dynamic instance = Activator.CreateInstance(item);
                modelBuilder.ApplyConfiguration(instance);
            }
            base.OnModelCreating(modelBuilder);
        }
        /// <summary>
        /// 获取设置项
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Type> getCurrentConfigTypes()
        {
            var types = Assembly.GetExecutingAssembly().GetTypes().Where(x => x.Name.EndsWith("Config"));
            return types.ToList();
        }
    }
}

